﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using SIS.ENT;
using SIS.BLL;
/// <summary>
/// Creator: Phyo Kyaw Thant (01/Feb/2012)
/// Updating More address options 
///     - Resident Address
///     - Contact Address
///     - Mailing Address
///     
/// 17/Apr/2012 (Phyo Kyaw Thant) - HPNY Myanmar New Year (Myanmar Calendar 1375 - 4 days Thingyan)
/// All reference dropdownlist updated (Created AddressDB, AddressManager)
///     - Nationality
///     - Country, District/Province, City
/// </summary>
public partial class student_Default : System.Web.UI.Page
{
    public SIS.UI.Common _common = new SIS.UI.Common();
    protected void Page_Load(object sender, EventArgs e)
    {
        lblTopMessage.Text = DateTime.Now.ToString();
        if (!Page.IsPostBack)
        {
            SIS.Utilities.General.LoadDayDropdownList(ddlDOBDay);
            SIS.Utilities.General.LoadMonthDropdownList(ddlDOBMonth, 2);
            SIS.Utilities.General.LoadYearDropdownList(ddlDOBYear, (DateTime.Now.Year - 85), DateTime.Now.Year, 1);

            SIS.Utilities.General.LoadDayDropdownList(ddlGuardianDOBDay);
            SIS.Utilities.General.LoadMonthDropdownList(ddlGuardianDOBMonth, 2);
            SIS.Utilities.General.LoadYearDropdownList(ddlGuardianDOBYear, (DateTime.Now.Year - 120), DateTime.Now.Year - 18, 1);

            _common.LoadNationalityDDL(ddlNationality, string.Empty);
            _common.LoadTravelDocDDL(ddlTravelDocType, string.Empty);
            _common.LoadBloodGroupDDL(ddlBloodGroup, string.Empty);
            _common.LoadDrivingLicenseTypeDDL(ddlDrivingLicenceType, string.Empty);
            _common.LoadParentStatusDDL(ddlParentStatus, string.Empty);
            _common.LoadSpeakLanguageDDL(ddlFirstLanguage, string.Empty);
            _common.LoadSpeakLanguageDDL(ddlHomeLanguage, string.Empty);
            _common.LoadRelationshipTypeDDL(ddlGuardianRelationshipType, string.Empty);
            _common.LoadRelationshipTypeDDL(ddlLiveWith, string.Empty);
            _common.LoadRaceCodeDDL(ddlRaceCode, string.Empty);
            _common.LoadReligionCodeDDL(ddlReligion, string.Empty);
            _common.LoadMartialStatusCodeDDL(ddlMaritalStatus, string.Empty);
            _common.LoadNoOfSibling(ddlNoOfSibling, string.Empty);

            _common.LoadCountryCodeDDL(ddlCountry, string.Empty);
            _common.LoadCountryCodeDDL(ddlPCountry, string.Empty);
            _common.LoadCountryCodeDDL(ddlMCountry, string.Empty);
            _common.LoadCountryCodeDDL(ddlGuardianAddressCountry, string.Empty);
            _common.LoadCountryCodeDDL(ddlSLCountry, string.Empty);

            ddlSchoolList.DataBind();

            if (ddlSchoolList.Items.Count > 0)
            {
                ddlSchoolList.Text = System.Configuration.ConfigurationManager.AppSettings["SchoolKeyDefault"].ToString();

                //ddlSchoolList.SelectedIndex = 0;
                //ddlSchoolList_SelectedIndexChanged(sender, e);
            }

            if (Request.Params["id"] != null)
            {
                int _id = -1;
                if (Int32.TryParse(Request.Params["id"].ToString(), out _id))
                {
                    LoadStudentInfo(_id);
                    btnRegister.Visible = true;
                }
                else
                {
                    lblTopMessage.Text = "Invalid student account ID.";
                }

                lblTopMessage.Text = DateTime.Now.ToString();
            }
            else if (Request.Params["enqid"] != null && Request.Params["locid"] != null && Request.Params["schid"] != null)
            {
                int _enqid = -1;
                if (Int32.TryParse(Request.Params["enqid"].ToString(), out _enqid))
                {
                    LoadFromEnquiry(_enqid);
                    btnRegister.Visible = true;
                }
                else
                {
                    lblTopMessage.Text = "Invalid enquiry ID.";
                }

                lblTopMessage.Text = DateTime.Now.ToString();
            }
            else
            {
                lblTopMessage.Text = DateTime.Now.ToString();
            }


            #region Test Dropdown List Selected Text
            //<asp:ListItem Text="Select" Value="-1"></asp:ListItem>
            //<asp:ListItem Text="Not Applicable" Value="0"></asp:ListItem>
            //<asp:ListItem Text="Motor Bike" Value="1"></asp:ListItem>
            //<asp:ListItem Text="Private Car/Vehicle" Value="2"></asp:ListItem>
            //<asp:ListItem Text="Public Truck/Professional" Value="3"></asp:ListItem>
            //<asp:ListItem Text="Others" Value="99"></asp:ListItem>

            //ddlDrivingLicenceType.Items.Clear();
            //ddlDrivingLicenceType.Items.Add(new ListItem("Select", "-1"));
            //ddlDrivingLicenceType.Items.Add(new ListItem("Not Applicable", "0"));
            //ddlDrivingLicenceType.Items.Add(new ListItem("Motor Bike", "1"));
            //ddlDrivingLicenceType.Items.Add(new ListItem("Private Car/Vehicle", "2"));
            //ddlDrivingLicenceType.Items.Add(new ListItem("Public Truck/Professional", "3"));
            //ddlDrivingLicenceType.Items.Add(new ListItem("Others", "99"));

            //System.Data.DataTable _dtDLType = new System.Data.DataTable("tblDLType");
            //_dtDLType.Columns.Add("DLValue");
            //_dtDLType.Columns.Add("DLText");

            //System.Data.DataRow _aRow = _dtDLType.NewRow();
            //_aRow[0] = "-1";
            //_aRow[1] = "Select";
            //_dtDLType.Rows.Add(_aRow);

            //System.Data.DataRow _dRow = _dtDLType.NewRow();
            //_dRow[0] = "0";
            //_dRow[1] = "Not Applicable";
            //_dtDLType.Rows.Add(_dRow);

            //System.Data.DataRow _bRow = _dtDLType.NewRow();
            //_bRow[0] = "1";
            //_bRow[1] = "Motor Bike";
            //_dtDLType.Rows.Add(_bRow);

            //System.Data.DataRow _cRow = _dtDLType.NewRow();
            //_cRow[0] = "2";
            //_cRow[1] = "Private Car/Vehicle";
            //_dtDLType.Rows.Add(_cRow);


            //System.Data.DataRow _eRow = _dtDLType.NewRow();
            //_eRow[0] = "3";
            //_eRow[1] = "Public Truck/Professional";
            //_dtDLType.Rows.Add(_eRow);


            //System.Data.DataRow _fRow = _dtDLType.NewRow();
            //_fRow[0] = "99";
            //_fRow[1] = "Others";
            //_dtDLType.Rows.Add(_fRow);

            //ddlDrivingLicenceType.DataSource = _dtDLType;
            //ddlDrivingLicenceType.DataValueField = "DLValue";
            //ddlDrivingLicenceType.DataTextField = "DLText";
            //ddlDrivingLicenceType.DataBind();
            #endregion

            #region TEST DATE TIME
            //DateTime _dtTest = DateTime.Now;
            //string _strDate = "01/03/2011";
            //if (SIS.Utilities.General.GetDateTime(_strDate, out _dtTest))
            //{
            //    lblMessage.Text += _strDate + " >> " + _dtTest.ToString() + "<br/>";
            //}
            //else
            //{
            //    lblMessage.Text += _strDate + " >> INVALID " + _dtTest.ToString() + "<br/>";
            //}

            //_strDate = "01/03/2011 10:00 PM";
            //if (SIS.Utilities.General.GetDateTime(_strDate, out _dtTest))
            //{
            //    lblMessage.Text += _strDate + " >> " + _dtTest.ToString() + "<br/>";
            //}
            //else
            //{
            //    lblMessage.Text += _strDate + " >> INVALID " + _dtTest.ToString() + "<br/>";
            //}


            //_strDate = "03/01/2011";
            //if (SIS.Utilities.General.GetDateTime(_strDate, out _dtTest))
            //{
            //    lblMessage.Text += _strDate + " >> " + _dtTest.ToString() + "<br/>";
            //}
            //else
            //{
            //    lblMessage.Text += _strDate + " >> INVALID " + _dtTest.ToString() + "<br/>";
            //}

            //_strDate = "Mar/03/2011";
            //if (SIS.Utilities.General.GetDateTime(_strDate, out _dtTest))
            //{
            //    lblMessage.Text += _strDate + " >> " + _dtTest.ToString() + "<br/>";
            //}
            //else
            //{
            //    lblMessage.Text += _strDate + " >> INVALID " + _dtTest.ToString() + "<br/>";
            //}
            #endregion
        }

    }

    private void LoadFromEnquiry(Int32 EnquiryKey)
    {
        EnquiryTable _enquiry = new EnquiryTable();
        _enquiry = EnquiryTableManager.GetItem(EnquiryKey);
        if (_enquiry != null)
        {
            //lblEnquiryKey.Text = _enquiryKey.ToString();
            ddlSchoolList.Text = _enquiry.SchoolKey.ToString();
            lblSchoolKey.Text = _enquiry.SchoolKey.ToString();
            //lblLocationKey.Text = _locationKey.ToString();
            if (_enquiry.RelationshipTypeCode == 0)
            {
                txtFirstName.Text = _enquiry.FirstName;
                txtLastName.Text = _enquiry.LastName;
                txtOtherName.Text = _enquiry.OtherName;

                //ddlGender.Text = _enquiry.Gender;
                //ddlEnquiryType.Text = _enquiry.EnquiryType.ToString();
                //ddlEnquiryReasonType.Text = _enquiry.EnquiryReasonType.ToString();
                //txtMessage.Text = _enquiry.Message;
                //txtRemark.Text = _enquiry.Remark;

                txtEmailAddress.Text = _enquiry.ContactEmail;
                txtMobileNumber.Text = _enquiry.ContactPhoneNumber;
                //ddlRelationShipType.Text = _enquiry.RelationshipTypeCode.ToString();
                //ddlPreferContactType.Text = _enquiry.PreferContactTypeCode.ToString();

                if (_enquiry.ContactAddress.Length > 0)
                {
                    string[] _address = _enquiry.ContactAddress.Split('|');
                    if (_address.Length == 6)
                    {
                        txtUnitNo.Text = _address[0];
                        txtStreetName.Text = _address[1];

                        ListItem _lstCity = ddlCity.Items.FindByText(_address[2]);
                        if (_lstCity != null)
                        {
                            ddlCity.Text = _lstCity.Value.ToString();
                        }

                        ListItem _lstDistrict = ddlDistrict.Items.FindByText(_address[3]);
                        if (_lstDistrict != null)
                        {
                            ddlDistrict.Text = _lstDistrict.Value.ToString();
                        }

                        ListItem _lstCountry = ddlCountry.Items.FindByText(_address[4]);
                        if (_lstCountry != null)
                        {
                            ddlCountry.Text = _lstCountry.Value.ToString();
                        }

                        txtPostCode.Text = _address[5];
                    }
                    else
                    {
                        txtStreetName.Text = _enquiry.ContactAddress;
                    }
                }
            }
            else
            {
                ddlGuardianRelationshipType.Text = _enquiry.RelationshipTypeCode.ToString();
                txtGuardianFirstName.Text = _enquiry.FirstName;
                txtGuardianLastName.Text = _enquiry.LastName;
                //txtGuardianOtherName.Text = _enquiry.OtherName;

                //ddlGender.Text = _enquiry.Gender;
                //ddlEnquiryType.Text = _enquiry.EnquiryType.ToString();
                //ddlEnquiryReasonType.Text = _enquiry.EnquiryReasonType.ToString();
                //txtMessage.Text = _enquiry.Message;
                //txtRemark.Text = _enquiry.Remark;

                txtGuardianEmailAddress.Text = _enquiry.ContactEmail;
                txtGuardianMobileNumber.Text = _enquiry.ContactPhoneNumber;
                //ddlRelationShipType.Text = _enquiry.RelationshipTypeCode.ToString();
                //ddlPreferContactType.Text = _enquiry.PreferContactTypeCode.ToString();

                if (_enquiry.ContactAddress.Length > 0)
                {
                    string[] _address = _enquiry.ContactAddress.Split('|');
                    if (_address.Length == 6)
                    {
                        txtGuardianAddressUnitNo.Text = _address[0];
                        txtGuardianAddressStreet.Text = _address[1];

                        ListItem _lstCity = ddlCity.Items.FindByText(_address[2]);
                        if (_lstCity != null)
                        {
                            ddlGuardianAddressCity.Text = _lstCity.Value.ToString();
                        }

                        ListItem _lstDistrict = ddlDistrict.Items.FindByText(_address[3]);
                        if (_lstDistrict != null)
                        {
                            ddlGuardianAddressDistrict.Text = _lstDistrict.Value.ToString();
                        }

                        ListItem _lstCountry = ddlCountry.Items.FindByText(_address[4]);
                        if (_lstCountry != null)
                        {
                            ddlGuardianAddressCountry.Text = _lstCountry.Value.ToString();
                        }

                        txtGuardianAddressPostal.Text = _address[5];
                    }
                    else
                    {
                        txtGuardianAddressStreet.Text = _enquiry.ContactAddress;
                    }
                }
            }
        }
    }

    private void LoadStudentInfo(int AccountID)
    {
        UserAccountTable _account = new UserAccountTable();
        _account = UserAccountTableManager.GetItem(AccountID);

        if (_account == null)
        {
            lblTopMessage.Text = "Student account ID (" + AccountID.ToString() + ") is not found!";
        }
        else if (_account.UserAccountKey <= 0)
        {
            lblTopMessage.Text = "Student account ID is not found!";
        }

        if (_account != null && _account.UserAccountKey > 0)
        {
            if (_account.UserAccountTypeCode != 300)
            {
                lblTopMessage.Text = "Only for student account ID will be loaded.";
                return;
            }

            lblUserAccountKey.Text = _account.UserAccountKey.ToString();
            hplGuardianDetail.Visible = true;
            hplGuardianDetail.NavigateUrl = "guardian-detail.aspx?id=" + lblUserAccountKey.Text;

            StudentProfileTable _sprofile = new StudentProfileTable();

            StudentAddressTable _sraddress = new StudentAddressTable();
            StudentAddressTable _spaddress = new StudentAddressTable();
            StudentAddressTable _smaddress = new StudentAddressTable();

            StudentContactTable _scontact = new StudentContactTable();
            StudentGuardianRelationshipTable _gsrelation = new StudentGuardianRelationshipTable();

            _sprofile = StudentProfileTableManager.GetItemByUserAccountKey(AccountID);
            if (_sprofile != null && _sprofile.StudentProfileKey > 0)
            {
                lblStudentProfileKey.Text = _sprofile.StudentProfileKey.ToString();
                txtFirstName.Text = _sprofile.FirstName;
                txtLastName.Text = _sprofile.LastName;
                txtOtherName.Text = _sprofile.OtherName;
                txtNickName.Text = _sprofile.NickName;

                ddlGender.Text = _sprofile.Gender;
                DateTime _dtDOB = _sprofile.DateOfBirth;
                ddlDOBDay.Text = _dtDOB.Day.ToString("00");
                ddlDOBMonth.Text = _dtDOB.Month.ToString("00");
                ddlDOBYear.Text = _dtDOB.Year.ToString("0000");

                txtNRICNumber.Text = _sprofile.NRICNumber;
                txtPlaceOfBirth.Text = _sprofile.PlaceOfBirth;
                ddlLiveWith.Text = _sprofile.LiveWith.ToString();
                ddlParentStatus.Text = _sprofile.ParentStatus.ToString();
                txtLegalCustobyName.Text = _sprofile.LegalCustobyName;
                ddlFirstLanguage.Text = _sprofile.FirstLanguageSpeak.ToString();
                ddlHomeLanguage.Text = _sprofile.LanguageSpeakAtHome.ToString();
                ddlNoOfSibling.Text = _sprofile.NoOfSibling.ToString();
                txtHobby.Text = _sprofile.Hobby;

                ddlDrivingLicenceType.Text = _sprofile.DrivingLicenceType.ToString();
                txtDrivingLicenceNumber.Text = _sprofile.DrivingLicenseNumber;

                ddlTravelDocType.Text = _sprofile.TravelDocumentType.ToString();
                txtTravelDocNumber.Text = _sprofile.TravelDocumentNumber;

                ddlBloodGroup.Text = _sprofile.BloodGroupCode.ToString();
                ddlMaritalStatus.Text = _sprofile.MartialStatusCode.ToString();
                ddlRaceCode.Text = _sprofile.RaceCode.ToString();
                ddlReligion.Text = _sprofile.ReligionCode.ToString();
                ddlNationality.Text = _sprofile.NationalityCode.ToString();
                txtWebsiteUrl.Text = _sprofile.WebSiteUrl;
                txtDisablityRemark.Text = _sprofile.DisabilityRemark;
                txtRemark.Text = _sprofile.Remark;

                if (_sprofile.Photo != null && _sprofile.Photo.Length > 0)
                {
                    divStudentPhoto.Visible = true;
                    imgCurrentStudentPhoto.ImageUrl = "~/images/imagehandler.ashx?key=" + _sprofile.StudentProfileKey.ToString() + "&type=studentphoto";
                    hplCurrentStudentPhoto.NavigateUrl = imgCurrentStudentPhoto.ImageUrl;
                    imgCurrentStudentPhoto.Width = 75;
                    imgCurrentStudentPhoto.Height = 100;
                }
                else
                {
                    divStudentPhoto.Visible = false;
                    imgCurrentStudentPhoto.ImageUrl = "";
                    hplCurrentStudentPhoto.NavigateUrl = "";
                    imgCurrentStudentPhoto.Width = 0;
                    imgCurrentStudentPhoto.Height = 0;
                }
                btnUpdatePhoto.Visible = true;
                tpOtherSkill.Enabled = true;
                tpHistory.Enabled = true;
            }

            //Resident
            _sraddress = StudentAddressTableManager.GetItemByUserAccountKey(AccountID, 1);
            if (_sraddress != null && _sraddress.StudentAddressKey > 0)
            {
                lblStudentRAddressKey.Text = _sraddress.StudentAddressKey.ToString();
                txtUnitNo.Text = _sraddress.UnitNo;
                txtStreetName.Text = _sraddress.StreetName;
                ddlCountry.Text = _sraddress.CountryCode.ToString();
                ddlCountry_SelectedIndexChanged(null, null);
                ddlDistrict.Text = _sraddress.DistrictCode.ToString();
                ddlDistrict_SelectedIndexChanged(null, null);
                ddlCity.Text = _sraddress.CityCode.ToString();
                txtPostCode.Text = _sraddress.PostalCode;
            }

            //Permanent/Contact
            _spaddress = StudentAddressTableManager.GetItemByUserAccountKey(AccountID, 2);
            if (_spaddress != null && _spaddress.StudentAddressKey > 0)
            {
                lblStudentPAddressKey.Text = _spaddress.StudentAddressKey.ToString();
                txtPUnitNo.Text = _spaddress.UnitNo;
                txtPStreetName.Text = _spaddress.StreetName;
                ddlPCountry.Text = _spaddress.CountryCode.ToString();
                ddlPCountry_SelectedIndexChanged(null, null);
                ddlPDistrict.Text = _spaddress.DistrictCode.ToString();
                ddlPDistrict_SelectedIndexChanged(null, null);
                ddlPCity.Text = _spaddress.CityCode.ToString();
                txtPPostCode.Text = _spaddress.PostalCode;
                chkPSameAsResident.Checked = _spaddress.SameAsResident;
                chkPSameAsResident_CheckedChanged(null, null);
            }

            //Mailing
            _smaddress = StudentAddressTableManager.GetItemByUserAccountKey(AccountID, 3);
            if (_smaddress != null && _smaddress.StudentAddressKey > 0)
            {
                lblStudentMAddressKey.Text = _smaddress.StudentAddressKey.ToString();
                txtMUnitNo.Text = _smaddress.UnitNo;
                txtMStreetName.Text = _smaddress.StreetName;
                ddlMCountry.Text = _smaddress.CountryCode.ToString();
                ddlMCountry_SelectedIndexChanged(null, null);
                ddlMDistrict.Text = _smaddress.DistrictCode.ToString();
                ddlMDistrict_SelectedIndexChanged(null, null);
                ddlMCity.Text = _smaddress.CityCode.ToString();
                txtMPostCode.Text = _smaddress.PostalCode;
                chkMSameAsResident.Checked = _smaddress.SameAsResident;
                chkMSameAsResident_CheckedChanged(null, null);
            }

            _scontact = StudentContactTableManager.GetItemByUserAccountKey(AccountID);
            if (_scontact != null && _scontact.StudentContactKey > 0)
            {
                lblStudentContactKey.Text = _scontact.StudentContactKey.ToString();
                txtMobileNumber.Text = _scontact.MobileNumber;
                txtHomePhoneNumber.Text = _scontact.HomePhoneNumber;
                txtFaxNumber.Text = _scontact.FaxNumber;
                txtOfficePhoneNumber.Text = _scontact.OfficePhoneNumber;
                txtEmailAddress.Text = _scontact.EmailAddress;
                txtOtherContactNumber.Text = _scontact.OtherNumber;
            }

            _gsrelation = StudentGuardianRelationshipTableManager.GetItemByStudentAccountKey(AccountID, true);

            if (_gsrelation != null && _gsrelation.GuardianAccountKey > 0)
            {
                lblStudentGuardianRelationTypeKey.Text = _gsrelation.GSAutoID.ToString();
                UserAccountTable _gaccount = new UserAccountTable();
                _gaccount = UserAccountTableManager.GetItem(_gsrelation.GuardianAccountKey);

                if (_gaccount != null && _gaccount.UserAccountKey > 0)
                {
                    lblGuardianAccountKey.Text = _gaccount.UserAccountKey.ToString();
                    GuardianProfileTable _gprofile = new GuardianProfileTable();
                    GuardianAddressTable _gaddress = new GuardianAddressTable();
                    GuardianContactTable _gcontact = new GuardianContactTable();

                    _gprofile = GuardianProfileTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey);
                    if (_gprofile != null && _gprofile.GuardianProfileKey > 0)
                    {
                        lblGuardianProfileKey.Text = _gprofile.GuardianProfileKey.ToString();
                        txtGuardianFirstName.Text = _gprofile.FirstName;
                        txtGuardianLastName.Text = _gprofile.LastName;
                        DateTime _dtGDOB = _gprofile.DateOfBirth;
                        ddlGuardianDOBDay.Text = _dtGDOB.Day.ToString("00");
                        ddlGuardianDOBMonth.Text = _dtGDOB.Month.ToString("00");
                        ddlGuardianDOBYear.Text = _dtGDOB.Year.ToString("0000");
                        ddlGuardianRelationshipType.Text = _gsrelation.RelationshipTypeCode.ToString();

                        txtGuardianOccupation.Text = _gprofile.Occupation;
                        txtGuardianDesignation.Text = _gprofile.Designation;
                        txtGuardianCompanyName.Text = _gprofile.CompanyName;
                        txtGuardianHighestEducation.Text = _gprofile.HighestEducation;
                    }

                    _gaddress = GuardianAddressTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey, 1);
                    if (_gaddress != null && _gaddress.GuardianAddressKey > 0)
                    {
                        lblGuardianAddressKey.Text = _gaddress.GuardianAddressKey.ToString();
                        txtGuardianAddressUnitNo.Text = _gaddress.UnitNo;
                        txtGuardianAddressStreet.Text = _gaddress.StreetName;
                        txtGuardianAddressPostal.Text = _gaddress.PostalCode;
                        ddlGuardianAddressCountry.Text = _gaddress.CountryCode.ToString();
                        ddlGuardianAddressCountry_SelectedIndexChanged(null, null);
                        ddlGuardianAddressDistrict.Text = _gaddress.DistrictCode.ToString();
                        ddlGuardianAddressDistrict_SelectedIndexChanged(null, null);
                        ddlGuardianAddressCity.Text = _gaddress.CityCode.ToString();
                        chkGuardianSameAsResident.Checked = _gaddress.SameAsStudentResidentAddress;
                        chkGuardianSameAsResident_CheckedChanged(null, null);
                    }

                    _gcontact = GuardianContactTableManager.GetItemByUserAccountKey(_gaccount.UserAccountKey);
                    if (_gcontact != null && _gcontact.GuardianContactKey > 0)
                    {
                        lblGuardianContactKey.Text = _gcontact.GuardianContactKey.ToString();
                        txtGuardianOtherContactNumber.Text = _gcontact.OtherNumber;
                        txtGuardianEmailAddress.Text = _gcontact.EmailAddress;
                        txtGuardianHomePhoneNumber.Text = _gcontact.HomePhoneNumber;
                        txtGuardianMobileNumber.Text = _gcontact.MobileNumber;
                        txtGuardianOfficePhoneNumber.Text = _gcontact.OfficePhoneNumber;
                    }
                }
            }
        }

    }

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btnShowStudentProfile_Click(object sender, EventArgs e)
    {

    }

    private bool CheckValidation()
    {
        bool _retValue = true;

        if (txtEmailAddress.Text.Trim().Length <= 0 &&
                txtMobileNumber.Text.Trim().Length <= 0 &&
                txtOtherContactNumber.Text.Trim().Length <= 0 &&
                txtHomePhoneNumber.Text.Trim().Length <= 0)
        {
            lblMessage.Text += "Student contacts are blank!<br/>";

            _retValue = false;
        }

        if (Convert.ToInt16(ddlGuardianRelationshipType.Text) <= 0 ||
                (txtGuardianFirstName.Text.Trim().Length <= 0 && txtGuardianLastName.Text.Trim().Length <= 0)
            || (txtGuardianHomePhoneNumber.Text.Trim().Length <= 0 && txtGuardianMobileNumber.Text.Trim().Length <= 0)
            || (chkGuardianSameAsResident.Checked == false && (txtGuardianAddressPostal.Text.Trim().Length <= 0 && Convert.ToInt16(ddlGuardianAddressCountry.Text) <= 0)))
        {
            lblMessage.Text += "Guardian's details are blank or not sufficient!<br/>";
            _retValue = false;
        }

        csvStudentContactNumbers.Validate();
        csvGuardianContactNumbers.Validate();
        csvStudentDOB.Validate();
        csvGuardianDOB.Validate();

        ChangeServerSideValidationEffect();
        lblTopMessage.Text = lblMessage.Text;
        return _retValue;
    }

    /// <summary>
    /// Saving all student details
    /// Check AccountKey and create new one if not found
    /// Saving Profile
    /// Saving Addresses
    /// Saving Contact
    /// Saving Guardian & Relationship type
    /// Saving Other Details
    /// Last Modified 25/Mar/2012 12:00 PM (Phyo Kyaw Thant)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// 

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        btnRegister.Visible = false;
        bool _status = true;
        Page.Validate("StudentProfile");
        if (Page.IsValid && CheckValidation())
        {
            UserAccountTable _account = new UserAccountTable();
            _account.UserAccountKey = Convert.ToInt32(lblUserAccountKey.Text);
            _account.SchoolKey = Convert.ToInt32(ddlSchoolList.Text);
            if (_account.UserAccountKey <= 0)
            {
                _account.UserAccountTypeCode = 300;
                _account.AuthCode = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6);
                _account.AuthSalt = "StudentGUID" + _account.AuthCode;

                _account.UserAccountAlternateKey = txtFirstName.Text.Replace(" ", ".") + txtLastName.Text.Replace(" ", ".") + "@" + ddlSchoolList.Text;
                lblUserAuthCode.Text = _account.UserAccountAlternateKey + " With " + _account.AuthCode + " salted " + _account.AuthSalt;
                _account.Status = -1;

                UserAccountTableManager.Save(_account).ToString();

                if (_account.UserAccountKey <= 0)
                {
                    lblMessage.Text = "Saving user account was failed!";
                    _status = false;
                    return;
                }
                else
                {
                    lblUserAccountKey.Text = _account.UserAccountKey.ToString();
                    hplGuardianDetail.Visible = true;
                    hplGuardianDetail.NavigateUrl = "guardian-detail.aspx?id=" + lblUserAccountKey.Text;
                }
            }

            //StudentProfileTableManager _sprofilemgr = new StudentProfileTableManager();
            StudentProfileTable _sprofile = new StudentProfileTable();

            //StudentContactTableManager _scontactmgr = new StudentContactTableManager();
            StudentContactTable _scontact = new StudentContactTable();

            //StudentAddressTableManager _saddressmgr = new StudentAddressTableManager();
            StudentAddressTable _sraddress = new StudentAddressTable();
            StudentAddressTable _spaddress = new StudentAddressTable();
            StudentAddressTable _smaddress = new StudentAddressTable();

            _sprofile.UserAccountKey = _account.UserAccountKey;
            _scontact.UserAccountKey = _account.UserAccountKey;
            _sraddress.UserAccountKey = _account.UserAccountKey;
            _spaddress.UserAccountKey = _account.UserAccountKey;
            _smaddress.UserAccountKey = _account.UserAccountKey;

            _sprofile.StudentProfileKey = Convert.ToInt32(lblStudentProfileKey.Text);
            _scontact.StudentContactKey = Convert.ToInt32(lblStudentContactKey.Text);
            _sraddress.StudentAddressKey = Convert.ToInt32(lblStudentRAddressKey.Text);
            _spaddress.StudentAddressKey = Convert.ToInt32(lblStudentPAddressKey.Text);
            _smaddress.StudentAddressKey = Convert.ToInt32(lblStudentMAddressKey.Text);

            _sprofile.FirstName = txtFirstName.Text;
            _sprofile.LastName = txtLastName.Text;
            _sprofile.NickName = txtNickName.Text;
            _sprofile.OtherName = txtOtherName.Text;
            _sprofile.Gender = ddlGender.Text;
            DateTime _dtDOB = DateTime.Now;
            string _strDOB = string.Empty;
            _sprofile.DateOfBirth = new DateTime(1900, 1, 1);


            _strDOB = ddlDOBDay.Text + "/" + ddlDOBMonth.SelectedItem.Text + "/" + ddlDOBYear.Text.ToString();
            if (SIS.Utilities.General.GetDateTime(_strDOB, out _dtDOB))
            {
                _sprofile.DateOfBirth = _dtDOB;
                //lblMessage.Text += "User DOB: " + _dtDOB.ToString("dd/MMM/yyyy") + "<br/>";
            }
            else
            {
                //lblMessage.Text += _strDOB + " " + "NO User DOB: " + _dtDOB.ToString("dd/MMM/yyyy") + "<br/>";
            }

            _sprofile.NRICNumber = txtNRICNumber.Text;

            _sprofile.PlaceOfBirth = txtPlaceOfBirth.Text;
            _sprofile.ParentStatus = Convert.ToInt16(ddlParentStatus.Text);

            _sprofile.LiveWith = Convert.ToInt16(ddlLiveWith.Text);
            _sprofile.LegalCustobyName = txtLegalCustobyName.Text;
            _sprofile.FirstLanguageSpeak = Convert.ToInt16(ddlFirstLanguage.Text);
            _sprofile.LanguageSpeakAtHome = Convert.ToInt16(ddlHomeLanguage.Text);
            _sprofile.NoOfSibling = Convert.ToSByte(ddlNoOfSibling.Text);
            _sprofile.Hobby = txtHobby.Text;

            _sprofile.DrivingLicenceType = Convert.ToInt16(ddlDrivingLicenceType.Text);
            _sprofile.DrivingLicenseNumber = txtDrivingLicenceNumber.Text;

            _sprofile.TravelDocumentType = Convert.ToInt16(ddlTravelDocType.Text);
            _sprofile.TravelDocumentNumber = txtTravelDocNumber.Text;

            _sprofile.BloodGroupCode = Convert.ToInt16(ddlBloodGroup.Text);
            _sprofile.MartialStatusCode = Convert.ToInt16(ddlMaritalStatus.Text);
            _sprofile.RaceCode = Convert.ToInt16(ddlRaceCode.Text);
            _sprofile.ReligionCode = Convert.ToInt16(ddlReligion.Text);
            _sprofile.NationalityCode = Convert.ToInt16(ddlNationality.Text);
            _sprofile.WebSiteUrl = txtWebsiteUrl.Text;
            _sprofile.DisabilityRemark = txtDisablityRemark.Text;
            _sprofile.Remark = txtRemark.Text;
            //_sprofile.UserAccountKey = -1;
            StudentProfileTableManager.Save(_sprofile);

            if (_sprofile.StudentProfileKey <= 0)
            {
                lblMessage.Text += "Saving profile was failed!<br/>";
                _status = false;
                //lblStudentProfileKey.Text = "-1";
            }
            else
            {
                lblStudentProfileKey.Text = _sprofile.StudentProfileKey.ToString();
                //lblMessage.Text += "Saving profile was succeeded!<br/>";
            }


            _scontact.EmailAddress = txtEmailAddress.Text;
            _scontact.MobileNumber = txtMobileNumber.Text;
            _scontact.HomePhoneNumber = txtHomePhoneNumber.Text;
            _scontact.FaxNumber = txtFaxNumber.Text;
            _scontact.OfficePhoneNumber = txtOfficePhoneNumber.Text;
            _scontact.OtherNumber = txtOtherContactNumber.Text;
            StudentContactTableManager.Save(_scontact);


            if (_scontact.StudentContactKey <= 0)
            {
                lblMessage.Text += "Saving contact was failed!<br/>";
                _status = false;
                //lblStudentContactKey.Text = "-1";
            }
            else
            {
                lblStudentContactKey.Text = _scontact.StudentContactKey.ToString();
            }

            //Resident Address
            _sraddress.UnitNo = txtUnitNo.Text;
            _sraddress.StreetName = txtStreetName.Text;
            _sraddress.CountryCode = Convert.ToInt16(ddlCountry.Text);
            _sraddress.DistrictCode = Convert.ToInt16(ddlDistrict.Text);
            _sraddress.CityCode = Convert.ToInt16(ddlCity.Text);
            _sraddress.PostalCode = txtPostCode.Text;
            _sraddress.AddressTypeCode = 1;
            _spaddress.SameAsResident = false;
            StudentAddressTableManager.Save(_sraddress);

            if (_sraddress.StudentAddressKey <= 0)
            {
                lblMessage.Text += "Saving Resident address was failed!<br/>";
                _status = false;
                //lblStudentAddressKey.Text = "-1";
            }
            else
            {
                lblStudentRAddressKey.Text = _sraddress.StudentAddressKey.ToString();
            }

            //Permanent Address
            _spaddress.SameAsResident = chkPSameAsResident.Checked;

            if (_spaddress.SameAsResident)
            {
                _spaddress.UnitNo = txtUnitNo.Text;
                _spaddress.StreetName = txtStreetName.Text;
                _spaddress.CountryCode = Convert.ToInt16(ddlCountry.Text);
                _spaddress.DistrictCode = Convert.ToInt16(ddlDistrict.Text);
                _spaddress.CityCode = Convert.ToInt16(ddlCity.Text);
                _spaddress.PostalCode = txtPostCode.Text;
            }
            else
            {
                _spaddress.UnitNo = txtPUnitNo.Text;
                _spaddress.StreetName = txtPStreetName.Text;
                _spaddress.CountryCode = Convert.ToInt16(ddlPCountry.Text);
                _spaddress.DistrictCode = Convert.ToInt16(ddlPDistrict.Text);
                _spaddress.CityCode = Convert.ToInt16(ddlPCity.Text);
                _spaddress.PostalCode = txtPPostCode.Text;
            }

            _spaddress.AddressTypeCode = 2;

            StudentAddressTableManager.Save(_spaddress);

            if (_spaddress.StudentAddressKey <= 0)
            {
                lblMessage.Text += "Saving Permanent address was failed!<br/>";
                _status = false;
                //lblStudentAddressKey.Text = "-1";
            }
            else
            {
                lblStudentPAddressKey.Text = _spaddress.StudentAddressKey.ToString();
            }

            //Mailing Address
            _smaddress.SameAsResident = chkMSameAsResident.Checked;
            if (_smaddress.SameAsResident)
            {
                _smaddress.UnitNo = txtUnitNo.Text;
                _smaddress.StreetName = txtStreetName.Text;
                _smaddress.CountryCode = Convert.ToInt16(ddlCountry.Text);
                _smaddress.DistrictCode = Convert.ToInt16(ddlDistrict.Text);
                _smaddress.CityCode = Convert.ToInt16(ddlCity.Text);
                _smaddress.PostalCode = txtPostCode.Text;
            }
            else
            {
                _smaddress.UnitNo = txtMUnitNo.Text;
                _smaddress.StreetName = txtMStreetName.Text;
                _smaddress.CountryCode = Convert.ToInt16(ddlMCountry.Text);
                _smaddress.DistrictCode = Convert.ToInt16(ddlMDistrict.Text);
                _smaddress.CityCode = Convert.ToInt16(ddlMCity.Text);
                _smaddress.PostalCode = txtMPostCode.Text;
            }

            _smaddress.AddressTypeCode = 3;
            StudentAddressTableManager.Save(_smaddress);

            if (_smaddress.StudentAddressKey <= 0)
            {
                lblMessage.Text += "Saving Mailing address was failed!<br/>";
                _status = false;
                //lblStudentAddressKey.Text = "-1";
            }
            else
            {
                lblStudentMAddressKey.Text = _smaddress.StudentAddressKey.ToString();
            }


            if (Convert.ToInt16(ddlGuardianRelationshipType.Text) > 0 &&
                (txtGuardianFirstName.Text.Trim().Length > 0 || txtGuardianLastName.Text.Trim().Length > 0))
            {
                UserAccountTable _gaccount = new UserAccountTable();
                _gaccount.UserAccountKey = Convert.ToInt32(lblGuardianAccountKey.Text);
                _gaccount.SchoolKey = Convert.ToInt32(ddlSchoolList.Text);
                if (_gaccount.UserAccountKey <= 0)
                {
                    _gaccount.UserAccountTypeCode = 700;
                    _gaccount.AuthCode = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6);
                    _gaccount.AuthSalt = "StudentGuardianGUID" + _gaccount.AuthCode;

                    _gaccount.UserAccountAlternateKey = txtGuardianFirstName.Text.Replace(" ", ".") + txtGuardianLastName.Text.Replace(" ", ".") + "@" + ddlSchoolList.Text;
                    lblGuardianAccountAuthCode.Text = _gaccount.UserAccountAlternateKey + " With " + _gaccount.AuthCode + " salted " + _gaccount.AuthSalt;

                    _gaccount.Status = -1;
                    UserAccountTableManager.Save(_gaccount);

                    if (_gaccount.UserAccountKey <= 0)
                    {
                        lblMessage.Text = "Saving guardian user account was failed!";
                        _status = false;
                        return;
                    }
                    else
                    {
                        lblGuardianAccountKey.Text = _gaccount.UserAccountKey.ToString();
                    }
                }

                //StudentGuardianRelationshipTableManager _gsrealationmgr = new StudentGuardianRelationshipTableManager();
                StudentGuardianRelationshipTable _gsrelation = new StudentGuardianRelationshipTable();
                _gsrelation.RelationshipTypeCode = Convert.ToInt16(ddlGuardianRelationshipType.Text);
                _gsrelation.GSAutoID = Convert.ToInt32(lblStudentGuardianRelationTypeKey.Text);
                _gsrelation.StudentAccountKey = _account.UserAccountKey;
                _gsrelation.GuardianAccountKey = _gaccount.UserAccountKey;
                _gsrelation.EmergencyContact = txtGuardianMobileNumber.Text.Trim().ToString().Length >= 0 ? txtGuardianMobileNumber.Text : txtGuardianHomePhoneNumber.Text;
                _gsrelation.Status = true;
                _gsrelation.IsMainGuardian = true;
                StudentGuardianRelationshipTableManager.Save(_gsrelation);

                if (_gsrelation.GSAutoID <= 0)
                {
                    lblMessage.Text = "Saving student and guardian relation status was failed!";
                    _status = false;
                    return;
                }
                else
                {
                    lblStudentGuardianRelationTypeKey.Text = _gsrelation.GSAutoID.ToString();
                }

                //GuardianProfileTableManager _gprofilemgr = new GuardianProfileTableManager();
                GuardianProfileTable _gprofile = new GuardianProfileTable();

                //GuardianContactTableManager _gcontactmgr = new GuardianContactTableManager();
                GuardianContactTable _gcontact = new GuardianContactTable();

                //GuardianAddressTableManager _gaddressmgr = new GuardianAddressTableManager();
                GuardianAddressTable _gaddress = new GuardianAddressTable();

                _gprofile.UserAccountKey = _gaccount.UserAccountKey;
                _gcontact.UserAccountKey = _gaccount.UserAccountKey;
                _gaddress.UserAccountKey = _gaccount.UserAccountKey;

                _gprofile.GuardianProfileKey = Convert.ToInt32(lblGuardianProfileKey.Text);
                _gcontact.GuardianContactKey = Convert.ToInt32(lblGuardianContactKey.Text);
                _gaddress.GuardianAddressKey = Convert.ToInt32(lblGuardianAddressKey.Text);

                _gprofile.FirstName = txtGuardianFirstName.Text;
                _gprofile.LastName = txtGuardianLastName.Text;

                _gprofile.DateOfBirth = new DateTime(1900, 1, 1);
                _gprofile.HighestEducation = txtGuardianHighestEducation.Text;
                _gprofile.Occupation = txtGuardianOccupation.Text;
                _gprofile.Designation = txtGuardianDesignation.Text;
                _gprofile.CompanyName = txtGuardianCompanyName.Text;
                _strDOB = ddlGuardianDOBDay.Text + "/" + ddlGuardianDOBMonth.SelectedItem.Text + "/" + ddlGuardianDOBYear.Text.ToString();
                if (SIS.Utilities.General.GetDateTime(_strDOB, out _dtDOB))
                {
                    _gprofile.DateOfBirth = _dtDOB;
                    //lblMessage.Text += "Guardian DOB: " + _dtDOB.ToString("dd/MMM/yyyy") + "<br/>";
                }
                else
                {
                    //lblMessage.Text += _strDOB + " " + "NO Guardian DOB: " + _dtDOB.ToString("dd/MMM/yyyy") + "<br/>";
                }

                _gprofile.Status = 1;
                GuardianProfileTableManager.Save(_gprofile);


                if (_gprofile.GuardianProfileKey <= 0)
                {
                    lblMessage.Text += "Saving guardian's profile was failed!<br/>";
                    _status = false;
                    //lblGuardianProfileKey.Text = "-1";
                }
                else
                {
                    lblGuardianProfileKey.Text = _gprofile.GuardianProfileKey.ToString();
                }

                _gcontact.EmailAddress = txtGuardianEmailAddress.Text;
                _gcontact.OtherNumber = txtGuardianOtherContactNumber.Text;
                _gcontact.MobileNumber = txtGuardianMobileNumber.Text;
                _gcontact.HomePhoneNumber = txtHomePhoneNumber.Text;
                _gcontact.OfficePhoneNumber = txtGuardianOfficePhoneNumber.Text;
                GuardianContactTableManager.Save(_gcontact);

                if (_gcontact.GuardianContactKey <= 0)
                {
                    lblMessage.Text += "Saving guardian's contact was failed!<br/>";
                    _status = false;
                    //lblGuardianContactKey.Text = "-1";
                }
                else
                {
                    lblGuardianContactKey.Text = _gcontact.GuardianContactKey.ToString();
                }

                _gaddress.SameAsStudentResidentAddress = chkGuardianSameAsResident.Checked;

                if (_gaddress.SameAsStudentResidentAddress)
                {
                    _gaddress.UnitNo = txtUnitNo.Text;
                    _gaddress.StreetName = txtStreetName.Text;
                    _gaddress.CountryCode = Convert.ToInt16(ddlCountry.Text);
                    _gaddress.DistrictCode = Convert.ToInt16(ddlDistrict.Text);
                    _gaddress.CityCode = Convert.ToInt16(ddlCity.Text);
                    _gaddress.PostalCode = txtPostCode.Text;
                }
                else
                {
                    _gaddress.UnitNo = txtGuardianAddressUnitNo.Text;
                    _gaddress.StreetName = txtGuardianAddressStreet.Text;
                    _gaddress.CountryCode = Convert.ToInt16(ddlGuardianAddressCountry.Text);
                    _gaddress.DistrictCode = Convert.ToInt16(ddlGuardianAddressDistrict.Text);
                    _gaddress.CityCode = Convert.ToInt16(ddlGuardianAddressCity.Text);
                    _gaddress.PostalCode = txtGuardianAddressPostal.Text;

                }

                _gaddress.AddressTypeCode = 1;

                _gaddress.Status = 1;

                GuardianAddressTableManager.Save(_gaddress);

                if (_gaddress.GuardianAddressKey <= 0)
                {
                    lblMessage.Text += "Saving guardian's address was failed!<br/>";
                    _status = false;
                    //lblGuardianAddressKey.Text = "-1";
                }
                else
                {
                    lblGuardianAddressKey.Text = _gaddress.GuardianAddressKey.ToString();
                }
            }

            if (lblMessage.Text.Length == 0)
            {
                lblTopMessage.Text = "Saving was succeeded at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt");
                lblMessage.Text = "Saving was succeeded at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt");

            }
            btnRegister.Visible = _status;

            if (lblStudentProfileKey.Text != "-1")
            {
                btnUpdatePhoto.Visible = true;
                tpHistory.Enabled = true;
                tpOtherSkill.Enabled = false;
            }
            else
            {
                btnUpdatePhoto.Visible = false;
                tpHistory.Enabled = false;
                tpOtherSkill.Enabled = false;
            }

        }

    }

    protected void btnAddNewStudent_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx");
    }

    protected void btnViewStudentList_Click(object sender, EventArgs e)
    {
        Response.Redirect("student-list.aspx");
    }

    protected void btnRegisterStudent_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(lblUserAccountKey.Text) > 0)
        {
            Response.Redirect("student-registration.aspx?id=" + lblUserAccountKey.Text.ToString());
        }
        else
        {
            lblTopMessage.Text = "Please save current student record.";
            lblMessage.Text = "Please save current student record.";
            btnRegister.Visible = false;
        }
    }

    protected void chkPSameAsResident_CheckedChanged(object sender, EventArgs e)
    {
        divPUnitNo.Visible = !chkPSameAsResident.Checked;
        divPStreetName.Visible = !chkPSameAsResident.Checked;
        divPCountry.Visible = !chkPSameAsResident.Checked;
        divPCity.Visible = !chkPSameAsResident.Checked;

        rfvPUnitNo.Enabled = !chkPSameAsResident.Checked;
        rfvPStreetName.Enabled = !chkPSameAsResident.Checked;
        rfvPCountry.Enabled = !chkPSameAsResident.Checked;
        rfvPDistrict.Enabled = !chkPSameAsResident.Checked;
        rfvPCity.Enabled = !chkPSameAsResident.Checked;
        rfvPPostCode.Enabled = !chkPSameAsResident.Checked;

        if (chkPSameAsResident.Checked)
        {
            chkPSameAsResident.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            chkPSameAsResident.ForeColor = System.Drawing.Color.Green;
        }
    }

    protected void chkMSameAsResident_CheckedChanged(object sender, EventArgs e)
    {
        divMUnitNo.Visible = !chkMSameAsResident.Checked;
        divMStreetName.Visible = !chkMSameAsResident.Checked;
        divMCountry.Visible = !chkMSameAsResident.Checked;
        divMCity.Visible = !chkMSameAsResident.Checked;

        rfvMUnitNo.Enabled = !chkMSameAsResident.Checked;
        rfvMStreetName.Enabled = !chkMSameAsResident.Checked;
        rfvMCountry.Enabled = !chkMSameAsResident.Checked;
        rfvMDistrict.Enabled = !chkMSameAsResident.Checked;
        rfvMCity.Enabled = !chkMSameAsResident.Checked;
        rfvMPostCode.Enabled = !chkMSameAsResident.Checked;

        if (chkMSameAsResident.Checked)
        {
            chkMSameAsResident.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            chkMSameAsResident.ForeColor = System.Drawing.Color.Green;
        }
    }

    protected void chkGuardianSameAsResident_CheckedChanged(object sender, EventArgs e)
    {
        divGuardianAddressUnitNo.Visible = !chkGuardianSameAsResident.Checked;
        divGuardianAddressStreet.Visible = !chkGuardianSameAsResident.Checked;
        divGuardianAddressCountry.Visible = !chkGuardianSameAsResident.Checked;
        divGuardianAddressCity.Visible = !chkGuardianSameAsResident.Checked;

        rfvGuardianAddressUnitNo.Enabled = !chkGuardianSameAsResident.Checked;
        rfvGuardianAddressStreet.Enabled = !chkGuardianSameAsResident.Checked;
        rfvGuardianAddressCountry.Enabled = !chkGuardianSameAsResident.Checked;
        rfvGuardianAddressDistrict.Enabled = !chkGuardianSameAsResident.Checked;
        rfvGuardianAddressCity.Enabled = !chkGuardianSameAsResident.Checked;
        rfvGuardianAddressPostal.Enabled = !chkGuardianSameAsResident.Checked;

        if (chkGuardianSameAsResident.Checked)
        {
            chkGuardianSameAsResident.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            chkGuardianSameAsResident.ForeColor = System.Drawing.Color.Green;
        }
    }

    private void ChangeServerSideValidationEffect()
    {
        if (csvGuardianContactNumbers.IsValid)
        {
            txtGuardianEmailAddress.ControlStyle.CssClass = "";
            txtGuardianHomePhoneNumber.ControlStyle.CssClass = "";
            txtGuardianMobileNumber.ControlStyle.CssClass = "";
            txtGuardianOtherContactNumber.ControlStyle.CssClass = "";
        }
        else
        {
            txtGuardianEmailAddress.ControlStyle.CssClass = "validationError";
            txtGuardianHomePhoneNumber.ControlStyle.CssClass = "validationError";
            txtGuardianMobileNumber.ControlStyle.CssClass = "validationError";
            txtGuardianOtherContactNumber.ControlStyle.CssClass = "validationError";
            lblMessage.Text += "Guardian's Contact Infomation are NOT sufficient!<br/>";
        }

        if (csvGuardianContactNumbers.IsValid)
        {
            txtEmailAddress.ControlStyle.CssClass = "";
            txtHomePhoneNumber.ControlStyle.CssClass = "";
            txtMobileNumber.ControlStyle.CssClass = "";
            txtOtherContactNumber.ControlStyle.CssClass = "";
        }
        else
        {
            txtEmailAddress.ControlStyle.CssClass = "validationError";
            txtHomePhoneNumber.ControlStyle.CssClass = "validationError";
            txtMobileNumber.ControlStyle.CssClass = "validationError";
            txtOtherContactNumber.ControlStyle.CssClass = "validationError";
        }

        if (csvStudentDOB.IsValid)
        {
            ddlDOBDay.ControlStyle.CssClass = "";
            ddlDOBMonth.ControlStyle.CssClass = "";
            ddlDOBYear.ControlStyle.CssClass = "";
        }
        else
        {
            ddlDOBDay.ControlStyle.CssClass = "validationError";
            ddlDOBMonth.ControlStyle.CssClass = "validationError";
            ddlDOBYear.ControlStyle.CssClass = "validationError";
        }

        if (csvGuardianDOB.IsValid)
        {
            ddlGuardianDOBDay.ControlStyle.CssClass = "";
            ddlGuardianDOBMonth.ControlStyle.CssClass = "";
            ddlGuardianDOBYear.ControlStyle.CssClass = "";
        }
        else
        {
            ddlGuardianDOBDay.ControlStyle.CssClass = "validationError";
            ddlGuardianDOBMonth.ControlStyle.CssClass = "validationError";
            ddlGuardianDOBYear.ControlStyle.CssClass = "validationError";
        }

    }

    protected void GuardianContactNumbers_ServerSideValidate(object source, ServerValidateEventArgs args)
    {
        bool _retValue = true;
        txtGuardianHomePhoneNumber.Text = txtGuardianHomePhoneNumber.Text.Trim();
        txtGuardianMobileNumber.Text = txtGuardianMobileNumber.Text.Trim();
        txtGuardianOtherContactNumber.Text = txtGuardianOtherContactNumber.Text.Trim();
        txtGuardianEmailAddress.Text = txtGuardianEmailAddress.Text.Trim();

        if (txtGuardianEmailAddress.Text.Length == 0 && txtGuardianHomePhoneNumber.Text.Length == 0 && txtGuardianMobileNumber.Text.Length == 0 && txtGuardianOtherContactNumber.Text.Length == 0)
        {
            args.IsValid = false;
        }
        else
        {

            args.IsValid = true;
        }
        args.IsValid = _retValue;
    }

    protected void StudentContactNumbers_ServerSideValidate(object source, ServerValidateEventArgs args)
    {
        bool _retValue = true;

        txtHomePhoneNumber.Text = txtHomePhoneNumber.Text.Trim();
        txtMobileNumber.Text = txtMobileNumber.Text.Trim();
        txtOtherContactNumber.Text = txtOtherContactNumber.Text.Trim();
        txtEmailAddress.Text = txtEmailAddress.Text.Trim();
        txtFaxNumber.Text = txtFaxNumber.Text.Trim();
        txtOfficePhoneNumber.Text = txtOfficePhoneNumber.Text.Trim();

        if (txtEmailAddress.Text.Length == 0 && txtHomePhoneNumber.Text.Length == 0 && txtMobileNumber.Text.Length == 0 && txtOtherContactNumber.Text.Length == 0 && txtOfficePhoneNumber.Text.Length == 0)
        {
            lblMessage.Text += "Student's Contact Infomation are NOT sufficient!<br/>";
            _retValue = false;
        }
        else
        {

            _retValue = true;
        }

        args.IsValid = _retValue;
    }

    protected void StudentDOB_ServerSideValidation(object source, ServerValidateEventArgs args)
    {
        bool _retValue = true;
        DateTime _dtDOB = DateTime.Now;
        string _strDOB = string.Empty;

        if (ddlDOBDay.Text != "-1" && ddlDOBMonth.Text != "-1" && ddlDOBYear.Text != "-1")
        {
            _strDOB = ddlDOBDay.Text + "/" + ddlDOBMonth.SelectedItem.Text + "/" + ddlDOBYear.Text.ToString();
            if (!SIS.Utilities.General.GetDateTime(_strDOB, out _dtDOB))
            {

                lblMessage.Text += _strDOB + "  " + " Student's DOB is invalid!<br/>";
                _retValue = false;
            }
            else
            {
                _retValue = true;
                //lblMessage.Text += "Student DOB: " + _dtDOB.ToString("dd/MMM/yyyy") + "<br/>";
            }
        }
        else
        {
            lblMessage.Text += "Select Student DOB.<br/>";
            _retValue = false;
        }
        args.IsValid = _retValue;
    }

    protected void GuardianDOB_ServerSideValidation(object source, ServerValidateEventArgs args)
    {
        bool _retValue = true;
        DateTime _dtDOB = DateTime.Now;
        string _strDOB = string.Empty;

        if (ddlGuardianDOBDay.Text != "-1" && ddlGuardianDOBMonth.Text != "-1" && ddlGuardianDOBYear.Text != "-1")
        {
            _strDOB = ddlGuardianDOBDay.Text + "/" + ddlGuardianDOBMonth.SelectedItem.Text + "/" + ddlGuardianDOBYear.Text.ToString();
            if (!SIS.Utilities.General.GetDateTime(_strDOB, out _dtDOB))
            {

                lblMessage.Text += "Guardian's DOB is invalid!<br/>";
                _retValue = false;
            }
            else
            {
                _retValue = true;
                //lblMessage.Text += "Guardian DOB: " + _dtDOB.ToString("dd/MMM/yyyy") + "<br/>";
            }
        }
        else
        {
            lblMessage.Text += "Select Guardian DOB.<br/>";
            _retValue = false;
        }

        args.IsValid = _retValue;
    }


    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadDistrictCodeDDL(ddlDistrict, Convert.ToInt16(ddlCountry.Text), string.Empty);
        ddlCity.Items.Clear();
    }

    protected void ddlSLCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadDistrictCodeDDL(ddlSLDistrict, Convert.ToInt16(ddlSLCountry.Text), string.Empty);
        ddlSLCity.Items.Clear();
    }

    protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadCityCodeDDL(ddlCity, Convert.ToInt32(ddlDistrict.Text), Convert.ToInt16(ddlCountry.Text), string.Empty);
    }

    protected void ddlSLDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadCityCodeDDL(ddlSLCity, Convert.ToInt32(ddlSLDistrict.Text), Convert.ToInt16(ddlSLCountry.Text), string.Empty);
    }

    protected void ddlPCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadDistrictCodeDDL(ddlPDistrict, Convert.ToInt16(ddlPCountry.Text), string.Empty);
    }

    protected void ddlPDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadCityCodeDDL(ddlPCity, Convert.ToInt32(ddlPDistrict.Text), Convert.ToInt16(ddlPCountry.Text), string.Empty);
    }

    protected void ddlMCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadDistrictCodeDDL(ddlMDistrict, Convert.ToInt16(ddlMCountry.Text), string.Empty);
    }

    protected void ddlMDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadCityCodeDDL(ddlMCity, Convert.ToInt32(ddlMDistrict.Text), Convert.ToInt16(ddlMCountry.Text), string.Empty);
    }

    protected void ddlGuardianAddressCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadDistrictCodeDDL(ddlGuardianAddressDistrict, Convert.ToInt16(ddlGuardianAddressCountry.Text), string.Empty);
    }

    protected void ddlGuardianAddressDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        _common.LoadCityCodeDDL(ddlGuardianAddressCity, Convert.ToInt32(ddlGuardianAddressDistrict.Text), Convert.ToInt16(ddlGuardianAddressCountry.Text), string.Empty);
    }
    protected void btnUpdatePhoto_Click(object sender, EventArgs e)
    {
        Response.Redirect("photo-upload.aspx?id=" + lblUserAccountKey.Text);
    }

    protected void btnSaveSkill_Click(object sender, EventArgs e)
    {
        if (lblUserAccountKey.Text != "-1")
        {

            Page.Validate("StudentOtherSkill");
            if (Page.IsValid)
            {
                StudentOtherSkillTable _otherSkill = new StudentOtherSkillTable();

                _otherSkill.SOSKey = Convert.ToInt32(lblSOSKey.Text);
                _otherSkill.UserAccountKey = Convert.ToInt32(lblUserAccountKey.Text);
                _otherSkill.SkillName = txtSkillName.Text;
                _otherSkill.OrganizationName = txtSkillOrgName.Text;
                _otherSkill.NoOfYear = Convert.ToByte(ddlSkillNoOfYear.Text);
                _otherSkill.SkillLevel = ddlSkillLevel.Text;
                _otherSkill.Description = txtSkillRemarkDesc.Text;

                if (StudentOtherSkillTableManager.Save(_otherSkill) > 0)
                {
                    grvStudentSkill.DataBind();
                    ClearSkillFields();
                    lblSkillMessage.Text = "Saved skill successfully at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt") + "!";
                }
                else
                {
                    lblSkillMessage.Text = "Saved failed successfully at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt") + "!";
                }
            }
            else
            {
                lblSkillMessage.Text = "Validation failed. Please complete required field(s).";
            }
        }
        else
        {
            lblSkillMessage.Text = "Please create a student profile firstly.";
        }
    }

    protected void ClearSkillFields()
    {
        lblSOSKey.Text = "-1";
        txtSkillName.Text = string.Empty;
        txtSkillOrgName.Text = string.Empty;
        ddlSkillNoOfYear.SelectedIndex = 0;
        ddlSkillLevel.SelectedIndex = 0;
        txtSkillRemarkDesc.Text = string.Empty;
        btnSaveSkill.Text = "Save";
        lblSkillMessage.Text = string.Empty;
    }

    protected void grvStudentSkill_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvStudentSkill.SelectedIndex >= 0)
        {
            ClearSkillFields();
            DataKey _selectedDataKey = grvStudentSkill.SelectedDataKey;
            int _SOSKey = Convert.ToInt32(_selectedDataKey[0]);
            int _studentAccountKey = Convert.ToInt32(_selectedDataKey[1]);

            if (_SOSKey > 0 && _studentAccountKey > 0)
            {
                StudentOtherSkillTable _otherSkill = new StudentOtherSkillTable();
                _otherSkill = StudentOtherSkillTableManager.GetItem(_SOSKey, _studentAccountKey);
                if (_otherSkill != null)
                {
                    lblSOSKey.Text = _otherSkill.SOSKey.ToString();
                    txtSkillName.Text = _otherSkill.SkillName;
                    txtSkillOrgName.Text = _otherSkill.OrganizationName;
                    ddlSkillNoOfYear.Text = _otherSkill.NoOfYear.ToString();
                    ddlSkillLevel.Text = _otherSkill.SkillLevel;
                    txtSkillRemarkDesc.Text = _otherSkill.Description;
                    btnSaveSkill.Text = "Update";
                }
            }
        }
        tbcEntry.ActiveTab = tpOtherSkill;
    }

    protected void btnClearOtherSkill_Click(object sender, EventArgs e)
    {
        ClearSkillFields();
    }

    private void ClearStudentSchoolFields()
    {
        lblSLSHKey.Text = "-1";
        txtSLStudentID.Text = string.Empty;
        txtSLSchoolName.Text = string.Empty;
        txtSLProgramName.Text = string.Empty;
        txtSLAffiliation.Text = string.Empty;
        txtSLStandardFrom.Text = string.Empty;
        txtSLStandardTo.Text = string.Empty;
        txtSLStartDate.Text = string.Empty;
        txtSLEndDate.Text = string.Empty;
        txtSLLastGrade.Text = string.Empty;
        txtSLWebsiteUrl.Text = string.Empty;
        txtSLEmailAddress.Text = string.Empty;
        txtSLContactNo.Text = string.Empty;
        txtSLUnitNo.Text = string.Empty;
        txtSLStreetName.Text = string.Empty;

        if (ddlSLCountry.Items.Count > 0)
        {
            ddlSLCountry.SelectedIndex = 0;
            ddlCountry_SelectedIndexChanged(null, null);
        }


        if (ddlSLDistrict.Items.Count > 0)
        {
            ddlDistrict.SelectedIndex = 0;
            ddlDistrict_SelectedIndexChanged(null, null);
        }

        if (ddlSLCity.Items.Count > 0)
        {
            ddlSLCity.SelectedIndex = 0;
        }
        
        txtSLPostCode.Text = string.Empty;
        txtSLRemark.Text = string.Empty;
        btnSaveSchool.Text = "Save";
    }

    protected void btnClearStudendSchool_Click(object sender, EventArgs e)
    {
        ClearStudentSchoolFields();
    }

    protected void grvSchoolHistory_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvSchoolHistory.SelectedIndex >= 0)
        {
            ClearStudentSchoolFields();
            DataKey _selectedDataKey = grvSchoolHistory.SelectedDataKey;
            int _LSSHKey = Convert.ToInt32(_selectedDataKey[0]);
            int _studentAccountKey = Convert.ToInt32(_selectedDataKey[1]);

            if (_LSSHKey > 0 && _studentAccountKey > 0)
            {
                StudentLastSchoolHistoryTable _lastSchool = new StudentLastSchoolHistoryTable();
                _lastSchool = StudentLastSchoolHistoryTableManager.GetItem(_LSSHKey, _studentAccountKey);
                if (_lastSchool != null)
                {
                    lblSLSHKey.Text = _lastSchool.SLSHKey.ToString();
                    txtSLStudentID.Text = _lastSchool.LastSchoolStudentID;
                    txtSLSchoolName.Text = _lastSchool.SchoolName;
                    txtSLProgramName.Text = _lastSchool.ProgramName;
                    txtSLAffiliation.Text = _lastSchool.Affiliation;
                    txtSLStandardFrom.Text = _lastSchool.FromStandardLevel;
                    txtSLStandardTo.Text = _lastSchool.ToStandardLevel;
                    txtSLStartDate.Text = _lastSchool.StartDate.ToString("dd/MMM/yyyy");
                    txtSLEndDate.Text = _lastSchool.EndDate.ToString("dd/MMM/yyyy");
                    txtSLLastGrade.Text = _lastSchool.LastGrade;
                    txtSLWebsiteUrl.Text = _lastSchool.SchoolWebsiteUrl;
                    txtSLEmailAddress.Text = _lastSchool.SchoolEmail;
                    txtSLContactNo.Text = _lastSchool.SchoolContact;
                    txtSLUnitNo.Text = _lastSchool.UnitNo;
                    txtSLStreetName.Text = _lastSchool.StreetName;
                    if (_lastSchool.CountryCode > 0)
                    {
                        ddlSLCountry.Text = _lastSchool.CountryCode.ToString();
                        ddlSLCountry_SelectedIndexChanged(sender, e);

                        if (_lastSchool.DistrictCode > 0)
                        {
                            ddlSLDistrict.Text = _lastSchool.DistrictCode.ToString();
                            ddlSLDistrict_SelectedIndexChanged(sender, e);
                            ddlSLCity.Text = _lastSchool.CityCode.ToString();
                        }

                    }
                    txtSLPostCode.Text = _lastSchool.PostalCode;
                    txtSLRemark.Text = _lastSchool.Remark;
                    btnSaveSchool.Text = "Update";
                }
            }
        }
        tbcEntry.ActiveTab = tpHistory;
    }

    protected void btnSaveSchool_Click(object sender, EventArgs e)
    {
        if (lblUserAccountKey.Text != "-1")
        {

            Page.Validate("StudentSchoolHistory");
            if (Page.IsValid)
            {
                StudentLastSchoolHistoryTable _lastSchool = new StudentLastSchoolHistoryTable();

                _lastSchool.SLSHKey = Convert.ToInt32(lblSLSHKey.Text);
                _lastSchool.UserAccountKey = Convert.ToInt32(lblUserAccountKey.Text);
                _lastSchool.LastSchoolStudentID = txtSLStudentID.Text;
                _lastSchool.SchoolName = txtSLSchoolName.Text;
                _lastSchool.ProgramName = txtSLProgramName.Text;
                _lastSchool.Affiliation = txtSLAffiliation.Text;
                _lastSchool.FromStandardLevel = txtSLStandardFrom.Text;
                _lastSchool.ToStandardLevel = txtSLStandardTo.Text;

                var dtFormatInfo = new DateTimeFormatInfo { ShortDatePattern = "dd-MMM-yyyy", DateSeparator = "/" };
                DateTime aTestDateTime = DateTime.Now;
                if (DateTime.TryParse(txtSLStartDate.Text, out aTestDateTime))
                {
                    DateTime SLStartDate = Convert.ToDateTime(txtSLStartDate.Text, dtFormatInfo);
                    if (SLStartDate != null)
                    {
                        _lastSchool.StartDate = SLStartDate;
                    }
                    else
                    {
                        _lastSchool.StartDate = new DateTime(1900, 1, 1);
                    }
                }
                else
                {
                    _lastSchool.StartDate = new DateTime(1900, 1, 1);
                }

                if (DateTime.TryParse(txtSLEndDate.Text, out aTestDateTime))
                {
                    DateTime SLEndDate = Convert.ToDateTime(txtSLEndDate.Text, dtFormatInfo);
                    if (SLEndDate != null)
                    {
                        _lastSchool.EndDate = SLEndDate;
                    }
                    else
                    {
                        _lastSchool.EndDate = new DateTime(1900, 1, 1);
                    }
                }
                else
                {
                    _lastSchool.EndDate = new DateTime(1900, 1, 1);
                }
                _lastSchool.LastGrade = txtSLLastGrade.Text;
                _lastSchool.SchoolWebsiteUrl = txtSLWebsiteUrl.Text;
                _lastSchool.SchoolEmail = txtSLEmailAddress.Text;
                _lastSchool.SchoolContact = txtSLContactNo.Text;
                _lastSchool.UnitNo = txtSLUnitNo.Text;
                _lastSchool.StreetName = txtSLStreetName.Text;

                if (Convert.ToInt16(ddlSLCountry.Text) > 0)
                {
                    _lastSchool.CountryCode = Convert.ToInt16(ddlSLCountry.Text);

                    if (Convert.ToInt16(ddlSLDistrict.Text) > 0)
                    {
                        _lastSchool.DistrictCode = Convert.ToInt16(ddlSLDistrict.Text);
                    }

                    if (Convert.ToInt16(ddlSLCity.Text) > 0)
                    {
                        _lastSchool.CityCode = Convert.ToInt16(ddlSLCity.Text);
                    }
                }

                _lastSchool.PostalCode = txtSLPostCode.Text;

                _lastSchool.Remark = txtSLRemark.Text;

                if (StudentLastSchoolHistoryTableManager.Save(_lastSchool) > 0)
                {
                    grvSchoolHistory.DataBind();
                    ClearStudentSchoolFields();
                    lblSkillMessage.Text = "Saved school successfully at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt") + "!";
                }
                else
                {
                    lblSkillMessage.Text = "Saved school successfully at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt") + "!";
                }
            }
            else
            {
                lblSkillMessage.Text = "Validation failed. Please complete required field(s).";
            }
        }
        else
        {
            lblSkillMessage.Text = "Please create a student profile firstly.";
        }
    }
}
